package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class OrderStatisticsTask {

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 每天凌晨2点统计前一天订单总金额以及订单数量
     */
    @Scheduled(cron = "0 0 2 * * ?")
    //@Scheduled(cron = "0/30 * * * * ?")  //方便测试
    public void orderStatisticsTask(){

        String orderDate = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");
        OrderStatistics orderStatistics = orderInfoMapper.orderTotalAmountStatistics(orderDate);
        if(orderStatistics!=null){ //前一天统计数据是存在订单的情况下，才会保存统计数据。否则不保存
            orderStatisticsMapper.insert(orderStatistics);
        }
    }
}
